package top.jolyoulu.codegenweb.controller.mysql;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.jolyoulu.codegenweb.entity.bo.form.DataSourceInfoFormBO;
import top.jolyoulu.codegenweb.service.bll.ApiMysqlDataSourceService;
import top.jolyoulu.webcommon.entity.ResultInfo;

import java.util.List;

/**
 * 数据源管理/mysql数据源操作
 */
@RestController
@RequestMapping("/api/mysql/data/source")
public class ApiMysqlDataSourceController {

    @Autowired
    private ApiMysqlDataSourceService apiMysqlDataSourceService;

    /**
     * 连接测试(新增/编辑)
     */
    @PostMapping("/test/connection")
    public ResultInfo<Boolean> testConnection(@RequestBody @Validated DataSourceInfoFormBO bo){
        boolean res = apiMysqlDataSourceService.testConnection(bo);
        return ResultInfo.success(res);
    }

    /**
     * 连接测试
     */
    @GetMapping("/{id}/test/connection")
    public ResultInfo<Boolean> testConnectionById(@PathVariable String id){
        boolean res = apiMysqlDataSourceService.testConnectionById(id);
        return ResultInfo.success(res);
    }

    /**
     * 获取数据库
     */
    @GetMapping("/{id}/schemas")
    public ResultInfo<List<String>> schemas(@PathVariable String id){
        List<String> res = apiMysqlDataSourceService.schemas(id);
        return ResultInfo.success(res);
    }

    /**
     * 获取表信息
     */
    @GetMapping("/{id}/tables")
    public ResultInfo<List<String>> tables(@PathVariable String id){
        List<String> res = apiMysqlDataSourceService.tables(id);
        return ResultInfo.success(res);
    }

}
